<?php

/*
 * File: levelup.php
 * Descrizione: Pagina di scelta del levelup.
 * Check: OK.
*/

// Avvia una nuova sessione o riprende una sessione gi� creata
session_start();

// Include i file necessari
include('inc/config.inc.php');
include('inc/template.inc.php');
include('inc/db.inc.php');
include('inc/check.inc.php');

// Recupera l'id dell'utente
$user_id = $_SESSION['uid'];

// Variabili.
$form_a1 = '';
$form_a2 = '';
$form_a3 = '';
$form_a4 = '';
$form_a5 = '';


	// Recupera le Abilit� del personaggio.
	$query = "SELECT * FROM abilitieslink WHERE user_id = '$user_id'";
	if($r1 = $db -> query("SELECT * FROM abilitieslink WHERE user_id = '$user_id'"))
	{
		$rd1 = $r1 -> fetch_assoc();
		
		$a1			= $rd1['ability_1'];
		$a2			= $rd1['ability_2'];
		$a3			= $rd1['ability_3'];
		$a4			= $rd1['ability_4'];
		$a5			= $rd1['ability_5'];
		$a_points	= $rd1['ability_points'];
		
		$r1 -> close();
	}
		
	// Se i punti abilit� sono maggiori di 0, mostra i tasti per aumentare le abilit�.
	if($a_points > 0)
	{
		$form_a1 .= '<input type="hidden" value="ability_1" name="selected"><button type="submit" class="btn btn-primary btn-xs pull-right" >&nbsp;<b class="glyphicon glyphicon-plus"></b></button>';
		$form_a2 .= '<input type="hidden" value="ability_2" name="selected"><button type="submit" class="btn btn-primary btn-xs pull-right" >&nbsp;<b class="glyphicon glyphicon-plus"></b></button>';
		$form_a3 .= '<input type="hidden" value="ability_3" name="selected"><button type="submit" class="btn btn-primary btn-xs pull-right" >&nbsp;<b class="glyphicon glyphicon-plus"></b></button>';
		$form_a4 .= '<input type="hidden" value="ability_4" name="selected"><button type="submit" class="btn btn-primary btn-xs pull-right" >&nbsp;<b class="glyphicon glyphicon-plus"></b></button>';
		
		// Se l'abilit� Avanzata (una volta ottenuta, viene inserita gi� con un punto) ha un valore diverso da 0, la mostra.
		if ($a5 != 0)
		{
			$form_a5	= '<input type="hidden" value="a5" name="selected"><button type="submit" class="btn btn-primary btn-xs pull-right" >&nbsp;<b class="glyphicon glyphicon-plus"></b></button>';
		} else {
			$form_a5 = '';
		}
	}

if($r2 = $db -> query("SELECT user_skillpoints, user_race, user_guild FROM users WHERE user_id = '$user_id'"))
{
	$rd2 = $r2 -> fetch_assoc();
				
	if($r3 = $db -> query("SELECT * FROM skills"))
	{
		$skillslist = '';
			
		while($rd3 = $r3 -> fetch_assoc())
		{
			//Variabili
			$skill_id = $rd3['skill_id'];

			if($r4 = $db -> query("SELECT user_id FROM skillslink WHERE user_id = '$user_id' AND skill_id = '$skill_id'"))
			{
				$c = $r4 -> num_rows;
				//Se la skill non � ancora stata imparata, la visualizza.
				if($c == 0)
				{
					$rd4 = $r4 -> fetch_assoc();
				
					if($r5 = $db -> query("SELECT guild_id, race_id FROM skills WHERE skill_id = '$skill_id'"))
					{
						$rd5 = $r5 -> fetch_assoc();
							
						// Controlli sull'assegnazione della skill a una gilda o una razza, e l'appartenenza del giocatore alla gilda o alla razza
						if(($rd2['user_skillpoints'] > 0) AND (($rd5['guild_id'] != 0 AND $rd2['user_guild'] == $rd5['guild_id']) OR ($rd5['race_id'] != 0 AND $rd2['user_race'] == $rd5['race_id']) OR ($rd5['guild_id'] == 0 AND $rd5['race_id'] == 0)))
						{
							$skillslist .= '<li class="list-group-item"><h4 class="list-group-item-heading"> ' . $rd3['skill_name'];
							$skillslist .= '</h4><p>' . $rd3['skill_desc'] . '<div class="pull-left"><form action="levelup-execute.php?act=2" method="post">';
							$skillslist .= '<button type="submit" class="btn btn-success">Apprendi</button><input type="hidden" name="selected" value="' . $skill_id . '"></form></div>';
							$skillslist .= '<div align="right">Costo della Skill:' . $rd3['skill_cost'] . '</div></p></li>';
						}
					}
				} else {
					$skillslist .= '<li class="list-group-item"><h4 class="list-group-item-heading"> ' . $rd3['skill_name'];
					$skillslist .= '</h4><p class="list-group-item-text">' . $rd3['skill_desc'] . '<div class="pull-left text-success"><b>Skill gi&agrave; appresa</b></form></div>';
					$skillslist .= '<div align="right">Costo della Skill:' . $rd3['skill_cost'] . '</div></p></li>';						
				}
			}
		}
	}
}
	// Lista delle tag speciali
	$tags = array(
					'temp_title' => $g_name, 'temp_a1name' => $g_a1name, 'temp_a2name' => $g_a2name, 
					'temp_a3name' => $g_a3name, 'temp_a4name' => $g_a4name, 'temp_a5name' => $g_a5name, 
					'temp_a1' => $a1, 'temp_a2' => $a2, 'temp_a3' => $a3, 'temp_a4' => $a4, 'temp_a5' => $a5, 
					'temp_formA1' => $form_a1, 'temp_formA1' => $form_a1, 'temp_formA2' => $form_a2, 
					'temp_formA3' => $form_a3, 'temp_formA4' => $form_a4, 'temp_formA5' => $form_a5, 'temp_points' => $a_points, 
					'temp_skillslist' => $skillslist
				);
				
	// Carica il template e formatta le tag speciali
	$temp = new template('style/levelup.template.html');
	$temp -> replace($tags);
	
	// Visualizza l'output
	echo $temp -> show();

// Chiude la connessione al database
$db -> close();

?>